<?php 

class suipaiModel extends appModel
{
	
    public $card1 = "plHTpwpHlVbKDhEpymBYgvXXIGtA";  //88
    public $card2 = "plHTpwi90bTyJWTcZtJ92eFHxSIU";
    public $card3 = "plHTpwpTNycK95kt4LxpfRk-rEE0";
    public $ex_card = "plHTpwmlhQqJ_6itfucIAlIcADaI";
    
    
    public function getCardList($order_id)
    {
        $sql = " SELECT * FROM ".$this->table("wx_pay"). 
               " WHERE wxid ='".$_SESSION['xywxid']."' AND id = '".$order_id."'";
        
       
        
        $back = $this->getRow($sql);
        
        if($back)
        {
            $card1_left = $back['card1_total'] - $back['card1_get'];
            $card2_left = $back['card2_total'] - $back['card2_get'];
            $card3_left = $back['card3_total'] - $back['card3_get'];
            
           
            
            $card_list = array();
            
            if($card1_left>0)
            {
                for($i=0;$i<$card1_left;$i++)
                {
                    
                    $card_list[]['cardId'] = $this->card1;
                }
            }
            if($card2_left>0)
            {
                for($i=0;$i<$card2_left;$i++)
                {
                    
                    $card_list[]['cardId'] = $this->card2;
                }
            }
            if($card3_left>0)
            {
                for($i=0;$i<$card3_left;$i++)
                {
                    $card_list[]['cardId'] = $this->card3;
                }
            }
            
//             if($card_list)
//             {
//                 $card_list[]['cardId'] = $this->ex_card;
//             }
            
            return $card_list;
        }
        else
        {
            return array();
        }
    }
    
	public function saveCard($cardList,$id)
	{
	    
	    
	    
	    $card1_num = 0;
	    $card2_num = 0;
	    $card3_num = 0;
	    $card_get  = 0;
	    
	    
	    foreach ($cardList as $k=>$v)
	    {
	       if($this->card1 == $v['cardId'])
	       {
	           $card1_num++;
	           $card_get++;
	       }
	       elseif($this->card2 == $v['cardId'])
	       {
	           $card2_num++;
	           $card_get++;
	       }
	       elseif($this->card3 == $v['cardId'])
	       {
	           $card3_num++;
	           $card_get++;
	       }
	       
	       
	    }
	    
	    $this->setInc("card1_get", $card1_num, "wx_pay", $id);
	    $this->setInc("card2_get", $card2_num, "wx_pay", $id);
	    $this->setInc("card3_get", $card3_num, "wx_pay", $id);
	    $this->setInc("get", $card_get, "wx_pay", $id);
	    
	}
	
	
	public function saveInfo($id)
	{
	     
	     
	    
	    $this->setWxInc("status", 1, "info_record", $id);
	   
	     
	}
	
	public function setInc($field,$step,$table,$id)
	{
	
	
	    $sql = " update ".$this->table($table)." set ".$field." = ".$field." + ".$step." where id = '".$id."'";
	
	    return $this->query($sql);
	}
	
	public function setWxInc($field,$step,$table,$id)
	{
	
	
	    $sql = " update ".$this->table($table)." set ".$field." = ".$field." + ".$step." where wxid = '".$id."'";
	
	    return $this->query($sql);
	}
	
	/**
	 *获得一条微信record记录
	 */
	public function getRecord($id)
	{
	    if(!$id)
	    {
	        return false;
	    }
	    $sql = "SELECT *,nickname as name FROM ".$this->table('wxusers')." WHERE wxid =".$id;
	    $result=$this->getRow($sql);
	
	    if($result)
	    {
	
	        return $result;
	    }
	    else
	    {
	        return false;
	    }
	}
	
	
	public function getList($wxid)
	{
		$time = date("YmdHis",time()-(120*60));
		$where = " AND (p1.time_start> $time or p1.pay_status='SUCCESS' )";
	    $sql = " SELECT * FROM ".$this->table('wx_pay')." AS p1 ".
	           " WHERE p1.wxid ='".$wxid."' $where ORDER BY p1.time_start desc ";
	    $result = $this->getAll($sql);
	    
	    foreach ($result as $k=>$v)
	    {
	        $created = strtotime($v['time_start']);
	        
	        $result[$k]['created'] = date("Y-m-d H:i:s",$created);
	        
	        if($v['total'] > $v['get'] && $v['total'] > 0)
	        {
	            $result[$k]['can'] = 1;
	        }
	        $result[$k]['total_fee'] = number_format($result[$k]['total_fee']/100,2);
	    }
	    
	    return $result;
	}
	
	
	public function isPaySucess($wxid){
		if(!$wxid){
			return false;
		}
		$sql=" SELECT count(*) FROM ".$this->table('wx_pay')." WHERE wxid='".$wxid."' AND pay_status='SUCCESS' AND total!=get ";
		$count = $this->getOne($sql);
		if($count){
			return true;
		}
		return  false;
		
	}
	
	public function is_saved_user($wxid){
	    $sql = "SELECT * FROM ".$this->table('info_record')." WHERE wxid = $wxid";
	    $count = $this->getRow($sql);
	    return $count;
	}
	
	
	public function get_card_name($card_id){
		if($this->card1==$card_id){
			return "成人门票";
		}elseif($this->card2==$card_id){
			return "慈善门票";
		}elseif($this->card3==$card_id){
			return "小童或长者门票";
		}elseif($this->ex_card==$card_id){
			return "体验馆门票";
		}else{
			return false;
		}
	}
	
	
	public function get_record($id)
	{
	    if(!$id)
	    {
	        return false;
	    }
	    $sql = " SELECT p2.*,nickname as name FROM ".$this->table('wxusers')." AS p1 "
	          ." LEFT JOIN ".$this->table('info_record')." AS p2  ON p2.wxid = p1.wxid WHERE p1.wxid ='".$id."'";
	    
	    $result=$this->getRow($sql);
	
	    if($result)
	    {
	
	        return $result;
	    }
	    else
	    {
	        return false;
	    }
	}
}

?>
